Hey there.
Welcome back.
Now that we have our guns completed, we need to address the next issue in our game, which is the fact
that our guns have a finite amount of ammo.
This means we need a script, a system where players can grab additional reserve ammo for their guns
when they run out.
That's why I have added these ammo crates around the map.
They're very simple models.
If we go ahead and take a look that just have a proximity prompt and a billboard guy inside of them,
and the plan is to listen for when this proximity prompt is triggered and then give ammo to the gun
our player currently has equipped.
There are three attributes in our ammo boxes which signify how much ammo is currently in the ammo box,
what's the maximum amount of ammo it can hold, and how long does it take to refill the ammo box once
it runs out?
Each of these ammo boxes are tagged with a string called Ammo Box.
If your ammo boxes are not tagged with the string, make sure to create a new tag and add that tag to
them all.
Otherwise, we can go ahead and create a new server script inside of our server script service.
We'll call this our ammo box handlers, and we can get started scripting the functionality for our ammo
boxes.
We'll be using the same template that we've always been using.
I'll just delete this section and delete this section.
And then we can go ahead and get the collection service in order to grab all of the ammo boxes in our
game.
And what we can go ahead and do is we can create a variable called ammo boxes.
And that will be equal to the collection service.
And we want to get tagged all of the ammo boxes or the instances that have the ammo box tag.
Then we would go ahead and loop through every single ammo box inside of our ammo boxes table.
And here we would listen for when the ammo box has its proximity prompt triggered.
We'd also want to listen for when the attributes on our ammo boxes changed, so that way we can update
the information being displayed in the billboard guys for our ammo boxes.
So let's go ahead and create a couple functions.
One is going to be to listen for when the proximity prompt is triggered.
So we'll call this on triggered.
And we're going to get past the player that triggered it.
And then we also want to know which ammo box had the proximity prompt triggered.
We also want to listen for when an attribute is changed on our ammo boxes.
We'll call it on attribute changed.
And again we want to know which ammo box had its attribute changed.
So we'll pass ammo box as well as the name of what attribute changed on our ammo box.
Then we can go ahead and refer to our ammo box to get the attachment that is inside of each one of them.
Get the proximity prompt and we want to listen to that proximity prompt triggered event.
And we'll connect a function, we'll get that player.
And here we will call our on triggered function.
We will pass the player as well as the box that got triggered.
We also want to listen to the attribute changed event.
For our ammo box.
We'll connect get the function and the name of the attribute that changed.
And then we'll call our on attribute change function.
Pass our ammo box and pass the name of the attribute that changed.
And then lastly, we want to be able to update the text inside of our billboard GIS to display how much
ammo is left inside of our ammo box.
So our billboard GIS are very simple.
It's just a text label inside of them that just says Ammo left and will update some text inside of them.
So since all of the text inside of our ammo boxes are blank, that means we want to go ahead and go
through every single ammo box and update their text to display how much ammo they have left.
So we'll refer to our box, get the attachment, get the billboard guy inside of them, get the text
label and update the text to something like we could say ammo left.
And then we're going to concatenate this and refer to the box and get the attribute of current ammo.
Now inside of the on triggered function.
What we want to go ahead and do is we want to check to see if the player that triggered this proximity
prompt actually has a gun equipped or a two on them.
So we'll create a variable called gun.
And we're going to refer to the player's character.
We're going to find first child, which is a tool.
If they do not have a tool equipped, then we're just going to return.
If this tool is not a gun, so if not gun get attribute gun type, then we're also going to return.
Otherwise what we can go ahead and do is we can go ahead and get the properties for our gun.
And that's going to be equal to require gun dot properties.
And this is because we want to check if the reserve ammo that's currently in the gun is not equal to
the maximum reserve ammo that our guns can hold.
So if gun get attribute ammo reserve, if that is equal to the properties.
And the property I believe is called Max reserve ammo, then we don't need to give the player any additional
reserve ammo because they're already full, so we'll just return.
Otherwise, we can go ahead and calculate how much ammo they need to fill up their ammo reserve.
So we'll call it ammo needed.
And that's going to be equal to.
And we're going to use the math dot minimum function.
And what we're going to do is we're going to get properties dot max reserve ammo.
And then we're going to subtract that by their current ammo reserve.
So gun get attribute ammo reserve.
And just in case they need more ammo than what is inside of our ammo box, the second value we're going
to pass here is ammo box.
Get attributes.
Current ammo, so we do not go above how much ammo is currently inside of our ammo box.
So once we've calculated how much ammo they need, then we can go ahead and update the attribute on
this gun set attribute.
Ammo reserve and we want to set it equal to gun get attribute ammo reserve.
And then we also want to add the ammo they need.
And then inside of our ammo box we want to go ahead and subtract the ammo they needed.
So ammo box set attribute current ammo.
And it's going to be equal to ammo box.
Get attribute current ammo and then subtract that by how much ammo we need.
And then from this point, this is where we would want to go ahead and play a sound, to make it sound
like the player reloaded their weapon with extra ammo.
So instead of replicated storage inside of shared and then inside of audio, I have a sound here called
Collect Ammo.
I'll play it for you.
So we want to play that sound when the player grabs ammo.
So we can go ahead and create a reference to this sound.
I'll just call it the Re-fill sound.
And that means we're going to have to grab replicated storage.
And then that's going to be equal to replicated storage dot shared dot audio dot collect ammo.
And then we can go ahead and clone this sound.
So clone is going to be equal.
To refill sound clone we'll set the parent to be equal to the ammo box.
And then we can go ahead and play that sound.
And then when that sound has ended, we'll go ahead and destroy it.
Cool.
Now, what we need to go ahead and do is we need to listen for when an attribute changes inside of our
ammo box.
In this case, the one we want to listen to is going to be the current ammo attribute.
So if the name of the attribute that changed is equal to current ammo.
Then what we want to do.
Is we want to go ahead and update the text label inside of our ammo box.
So we'll just copy this code down here, paste it right there, and then let me update this with ammo
box.
And then I'll update it right here as well.
And then we want to check if the current ammo that's inside of our ammo box is equal to zero.
If it is, then that means we need to go ahead and refill the ammo box.
So if ammo box get attribute current ammo, if that is equal to zero, then what we're going to do is
we're going to disable the proximity prompt inside of the ammo box.
So players can't interact with it anymore.
So ammo box, dot attachment dot proximity prompt dot enable.
We're going to set that to false.
And then here's where we're going to wait for that refill cooldown.
So we'll get our ammo box, get attributes, refill cooldown, and then we're going to wait for that
duration.
And then after that duration is up, we can go ahead and set on our ammo box set attribute.
Current ammo, and that's going to be equal to the ammo box.
Get attribute Max ammo.
And then once that's done, we can go ahead and set the proximity prompt to be enabled again.
Now, another thing we can do just in case, is inside of our on triggered function.
We can go ahead and check to see and make sure that we actually have enough ammo inside of our ammo
box.
So what we can go ahead and do is we can check at the top if our ammo box get attribute current ammo.
If that's equal to zero, then we're just going to return as well because we have no ammo to give this
player.
This is just in case because this function shouldn't execute since we're going to be disabling the proximity
prompt.
But as I said, just in case, we're going to put this in that function as well.
And now I believe that's all we need to do to set up our ammo boxes.
So to go ahead and test this out, I'll go into my server storage, grab one of my guns, copy that
and paste it into my starter pack.
And then we can go and play test the game.
No errors in the console, which is always good.
And then let's go walk over to one of our ammo boxes.
And as you can see, it currently displays how much ammo is left inside of the ammo box.
So if I get rid of one bullet and then I reload my gun.
So I have one bullet missing in my reserve ammo.
If I go ahead and use the ammo box to refill my gun.
As you can see, it subtracted one bullet out of my ammo box and placed that inside of the reserve ammo
for my gun.
Very cool.
So again, I need 40 bullets for my reserve ammo.
So if I go ahead and use this ammo box here.
As you can see, it subtracted 40 bullets from my ammo box and placed those 40 bullets inside of my
ammo reserve.
Very cool.
Now what I'm going to do is I'm going to duplicate one of these ammo piles and then place it over at
my spawn.
And then I'm going to update the attribute of current ammo.
And I'm just going to set that equal to one.
And we can go and playtest our game, because I want to see if it successfully refills my ammo box after
I take all the ammo out of it.
So let me shoot my gun and reload and let me go ahead and take this last bullet that is inside of this
animal pile.
And as you can see, there's no more ammo left.
And the proximity prompt disappeared.
So all we have to do now is wait to see how long it takes for this to refill the ammo inside of it,
so I'll catch you when it refills.
And just like that, our ammo pile has refilled itself and the proximity prompt has been re-enabled.
So now if I get rid of some of the ambience out of my gun, and then I go ahead and use this ammo pile,
hopefully, as you can see, perfect.
It still works.
So subtracted some ammo out of it and gave it to me in my gun.
Very cool.
Great job so far with this project.
I hope it is thoroughly challenging for you and I'll see you in the next lecture.